<template>
  <view class="padding-bottom-20">
    <by-background />

    <by-navigation-bar :title="title"/>

    <view class="padding-lr-20">
      <template v-if="list.length">
        <by-list-card
            v-for="(item,index) in list"
            :key="item.uid + index"
            :item="item.technician"
            :type="type"
            :date="dateFormat(item.createTime,'YYYY年MM月DD日 hh:mm:ss')"
            @show-menu="handleShowMenu(item.serialNo)"
            @showSafeguard="showSafeguard = true"
            @touch="showContact($event, item.tno)"
            @waitTouch="showDialg = true"
        />
        <by-load-footer :has-more="hasMore" text="加载中..." />
      </template>

      <by-nodata v-else />
    </view>
  </view>

  <by-page-dialogs
    v-model:show-touch="showTouch"
    v-model:show-contact-way="showContactWay"
    v-model:show-error="showError"
    v-model:show-safeguard="showSafeguard"
    :point="point"
    :tno="tno"
    @showContact="showContactWay = true"
  />

  <by-dialog v-model="showDialg" title="温馨提示" content="请耐心等待对方联系" confirm-text="知道了" :show-cancel="false"/>

  <by-draw-bottom v-model="showMenu" :watch-back="false" :list="menuList" @click="handleClick"/>
</template>

<script setup lang="ts">
import { userPayHistory, getTechnicianContactHistory, deleteUserContactRecord, technicianDeleteUserContactRecord } from '@/common/api'
import { dateFormat, GetList, toast } from '@/common/util'
import type { AnyObject, IGetList } from '@/common/types'

const showTouch = ref(false)
const showContactWay = ref(false)
const showError = ref(false)
const showSafeguard = ref(false)
const showMenu = ref(false)
const point = ref(0)
const tno = ref('')

const title = ref('')
const type = ref('')
const showDialg = ref(false)
onLoad((e) => {
  title.value = e?.type ? '收到联系' : '联系记录'
  type.value = e?.type ? 'receive' : 'history'
  const api = e?.type ? getTechnicianContactHistory : userPayHistory
  getList = new GetList(api, {}, res => res.records)
  reload()
})

const menuList = [
  { text: '删除记录' }
]

let serialNo:string
function handleShowMenu(id:string) {
  showMenu.value = true
  serialNo = id
}

async function handleClick() {
  if (type.value === 'history') {
    await deleteUserContactRecord({ serialNo })
  } else {
    await technicianDeleteUserContactRecord({ serialNo })
  }
  reload()
  toast('操作成功')
  showMenu.value = false
}

function showContact(e:AnyObject, id:string) {
  tno.value = id
  if (e.unlock) {
    showContactWay.value = true
  } else {
    point.value = e.unlockPoint
    if (e.canGet) showTouch.value = true
    else showError.value = true
  }
}

/* 获取列表*/
const hasMore = ref(true)
const list = shallowRef<AnyObject[]>([])
let getList:IGetList<AnyObject, AnyObject>
async function reload() {
  list.value = await getList.reload()
  hasMore.value = getList._has_more
}
async function nextPage() {
  hasMore.value = getList._has_more
  list.value.push(...await getList.nextPage())
  triggerRef(list)
}
onPullDownRefresh(reload)
onReachBottom(nextPage)

/* 监听返回*/
const back = computed(() => showTouch.value || showContactWay.value || showError.value || showSafeguard.value || showMenu.value || showDialg.value)
onBackPress(() => {
  if (back.value) {
    close()
    return true
  }
})
function close() {
  showTouch.value = showContactWay.value = showError.value = showSafeguard.value = showMenu.value = showDialg.value = false
}
</script>

<style scoped lang="scss">

</style>
